<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class UserInfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //显示
         $sess = session('homeuser');
          $id = $sess->id;
          $links=DB::table('links')->get();
        
        // $user=DB::table('user')->get();
        $db = DB::table('user')->where('id',$id)->first();
         $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
        return view('home.userinfo',['userinfo'=>$db,'links'=>$links,'search'=>$ss,
         'all'=>$type]); 
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //前台注册页面处理
        $messages = [
              'login_name.required' => '不能为空',
             'password.required' => '不能为空',
            ];

        $this->validate($request, [
          'login_name' => 'required|unique:user|max:16',
          'password' => 'required|alpha_num:6,16',
       ],$messages);
 

        $data = $request->except('_token','editpassword');
        $m = DB::table('user')->insertGetId($data);
        if($m>0){
            return  redirect('/home/gologin');
        }else{
            return  redirect('/home/goregister');
        }
           

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
     //显示安全中心页面
           $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
      $sess = session('homeuser');
          $id = $sess->id;
          $user = $sess->password;
          $links=DB::table('links')->get();
        return view('home.editpassword',['userinfo'=>$user,'id'=>$id,'links'=>$links,'search'=>$ss,
         'all'=>$type]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   
      //修改后的数据更新数据库
        if($request->has('password')){
            $data = $request->only('password');;
            $row = DB::table('user')->where('id',$id)->update($data);
            $a=$request->oldpassword;
            $b=$request->password;  
            if( $a != $b){
                return redirect('/home/gologin');
            }else{
                 return back()->with('msg','新密码与原密码相同'); 
            }
         }
       //父模板的首页登录名变化
        if($request->has('login_name')){
            $data = $request->except('_token','_method','password');
            $row = DB::table('user')->where('id',$id)->update($data);
           $a=($request->login_name);
           $b=($request->nick_name);
          if($a != $b){
             session('homeuser')->nick_name=$b;
             return redirect('/home');
          }
             if($row>0){
                return redirect('/home');
            }else{
                return back()->with('msg','信息无更改');
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
